import React, { PureComponent, Fragment } from 'react';
import { message } from 'antd';
import { connect } from 'dva';
import { DATA_ENTRY, AdvancedSearch, FirstHeaderC, ContentPackage, NoQueryC, TableC } from '../../../comps';
import _ from 'lodash';
import moment from 'moment';
import { findValueByCode } from '../../../utils/utils';

@connect(({ investmentAdviserReportM, loading, globalM }) => ({
  investmentAdviserReportM,
  loading: loading.effects['investmentAdviserReportM/queryReportTable'],
  dictMap: globalM.dataDictionary,
}))
export default class index extends PureComponent {
  state = {
    showTable: false,
    searchType: '1',
  };
  componentDidMount() {
    //获取交易日
    this.props.dispatch({
      type: 'investmentAdviserReportM/getTradingDay',
      payload: { type: '1' },
      callback: (data) => {
        this.setState({
          tradingday: data,
        });
        this.singleDateRef?.setDate(data ? moment(data) : moment().subtract(1, 'day'));
      },
    });
    this.props.dispatch({
      type: 'investmentAdviserReportM/getInvestmentAdviserInfo',
      payload: {},
    });
  }
  // 点击查询
  handlesearch = (params) => {
    if (!params.tradingday) {
      message.warning('请选择日期');
      return;
    }
    if (params.type === '1' && !params.product) {
      message.warning('请选择产品');
      return;
    }
    if (params.type === '2' && !params.adviser) {
      message.warning('请选择投顾');
      return;
    }
    let tempParams = {
      queryKey: 'InvestmentAdviserReport',
      queryConditions: {
        ...params,
        tradingday: params.tradingday.format('YYYY-MM-DD'),
        type: params.type,
        code: params.type == '1' ? params.product : params.adviser,
      },
    };
    this.setState({
      searchType: params.type,
      showTable: true,
    });
    this.props.dispatch({
      type: 'investmentAdviserReportM/queryReportTable',
      payload: {
        ...tempParams,
      },
    });
  };
  // 查询维度变化
  typeChange = (value) => {
    this.setState({
      searchType: value,
    });
  };
  // 重置查询条件
  resetForm = () => {
    this.singleDateRef?.setDate(this.state.tradingday ? moment(this.state.tradingday) : moment().subtract(1, 'day'));
    this.setState({
      showTable: false,
      searchType: '1',
    });
  };
  // 展开或收起
  onExpand = (expanded, record) => {
    const {
      investmentAdviserReportM: { expandedRowKeys },
    } = this.props;
    let expandedId = expandedRowKeys;
    if (expanded) {
      expandedId.push(record.key);
    } else {
      expandedId = expandedId.filter((item) => item != record.key);
    }
    this.props.dispatch({
      type: 'investmentAdviserReportM/save',
      payload: {
        expandedRowKeys: expandedId,
      },
    });
  };
  // 查询条件
  formRender = () => {
    const { searchType } = this.state;
    const {
      investmentAdviserReportM: { adviserList },
    } = this.props;
    const searchProps = {
      dataSource: [
        {
          dataIndex: 'tradingday',
          title: '日期',
          type: DATA_ENTRY.SINGLEDATE,
          // initialValue: moment().subtract(1,'day'),
          allowClear: false,
          disableEnd: moment().add(1, 'day'),
          onRef: (ref) => (this.singleDateRef = ref),
        },
        {
          dataIndex: 'type',
          title: '查询维度',
          type: DATA_ENTRY.SELECT,
          dataSource: [
            { title: '产品维度', value: '1' },
            { title: '投顾维度', value: '2' },
          ],
          initialValue: '1',
          allowClear: false,
          onChange: (e) => {
            this.typeChange(e);
          },
        },
        searchType == '1'
          ? {
              dataIndex: 'product',
              title: '产品名称',
              type: DATA_ENTRY.PRODUCTLIST,
              allowClear: true,
              dataType: '2',
              onRef: (ref) => (this.productRef = ref),
              tradeModel: ['BOOKKEEPING_MODE', 'COMMAND_MODE', 'VALUATION_BOOKKEEPING_MODEL'], //不支持选估值解析模式的产品
            }
          : {
              dataIndex: 'adviser',
              title: '投顾名称',
              type: DATA_ENTRY.SELECT,
              dataSource: adviserList,
              allowClear: true,
              onRef: (ref) => (this.adviserRef = ref),
            },
        {
          dataIndex: 'isShowSortSales',
          title: '是否展示卖空资产',
          type: DATA_ENTRY.SELECT,
          initialValue: '1',
          isTrueFalseJudge: true,
          allowClear: false,
        },
      ],
      onQuery: this.handlesearch,
      onClear: this.resetForm,
    };
    return <AdvancedSearch {...searchProps} />;
  };
  render() {
    const { showTable } = this.state;
    const {
      investmentAdviserReportM: { dataSource, columns, expandedRowKeys },
      loading,
      dictMap,
    } = this.props;
    const newColums = _.cloneDeep(columns);
    if (newColums?.length) {
      newColums.forEach((item) => {
        if (item.key === 'product') {
          item.render = (text, record) => {
            if (record.productCode) {
              return `${record.productCode}-${record.productName}`;
            }
            return '';
          };
        }
        if (item.key === 'adviserid') {
          item.render = (text, record) => {
            if (text && record.type == '2') {
              return text;
            }
            return '';
          };
        }
        if (item.key === 'secutype') {
          item.render = (text, record) => {
            if (record.secutype) {
              return findValueByCode(record.secutype, dictMap['security_bond_type']);
            }
            return '';
          };
        }
      });
    }
    return (
      <ContentPackage
        title='投顾报告'
        contentHtml={
          <>
            {this.formRender()}
            <div style={{ marginTop: '20px' }}>
              {showTable ? (
                <TableC
                  dataSource={dataSource}
                  loading={loading}
                  columns={newColums}
                  onExpand={this.onExpand}
                  expandedRowKeys={expandedRowKeys}
                  hidePage={true}
                  rowKey={'key'}
                  selfScrollY={true}
                />
              ) : (
                <NoQueryC></NoQueryC>
              )}
            </div>
          </>
        }
      />
    );
  }
}
